package com.jufa_3.controller;

import com.alibaba.fastjson.JSONObject;
import com.jufa_3.common.R;
import com.jufa_3.dto.ComboCostInfo;
import com.jufa_3.entity.ComboOrder;
import com.jufa_3.mapper.ComboOrderMapper;
import com.jufa_3.service.ComboOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/comboOrder")
public class ComboOrderController {

    @Autowired
    private ComboOrderService comboOrderService;

    @Autowired
    private ComboOrderMapper comboOrderMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/getShareMonly")
    public R<JSONObject> getShareMonly() {
        JSONObject np = new JSONObject();
        np.put("newUserPrice", redisTemplate.opsForValue().get("newUserPrice"));
        np.put("pullUserPrice", redisTemplate.opsForValue().get("pullUserPrice"));
        return R.success(np);
    }

    @GetMapping("/updataComboOrder")
    public R<Boolean> updataComboOrder(ComboOrder co) {
        return R.success(comboOrderService.updateById(co));
    }

    /**
     * 获取用户订购的套餐
     *
     * @return
     */
    @GetMapping("/getYueCombo")
    public R<ComboCostInfo> getYueCombo(String openId) {
        ComboCostInfo userCombos = comboOrderService.getUserCombos(openId, false);
        if (userCombos != null) {
            return R.success(userCombos);
        } else {
            return R.error(4011, "未订购套餐！");
        }
    }

    /**
     * 得到充值记录记录
     */
    @GetMapping("/getTopUpRecords")
    public R<Object> getTopUpRecords(ComboOrder comboOrder) {
        List<ComboOrder> payOrders = comboOrderMapper.getTopUpRecords(comboOrder);
        if (payOrders == null || payOrders.isEmpty()) {
            return R.success(1003, null);
        } else {
            return R.success(payOrders);
        }
    }

}
